package org.fig.dependency.injection;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Description: 基于API配置的 Setter注入
 * @Author: Flynn Vanderboom
 * @Data: 2021/6/20 22:18
 */

public class ApiDependencyConstructorInjectionDemo {
    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //生成  UserHolder 的 BeanDefinition
        BeanDefinition userHolderBeanDefinition = createUserHolderBeanDefinition();
        //注册  UserHolder 的 BeanDefinition
        context.registerBeanDefinition("userHolder",userHolderBeanDefinition);

        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);

        String xmlResourcePath = "classpath:META-INF/dependency-lookup-context.xml";

        //加载XML资源，解析并且生成BeanDefinition
        reader.loadBeanDefinitions(xmlResourcePath);



        //启动上下文
        context.refresh();

        
        UserHolder userHolder = context.getBean(UserHolder.class);

        System.out.println(userHolder);

        //关闭上下文
        context.close();
    }


    /***
     * 为 {@link UserHolder} 生成 {@link BeanDefinition}
     * @return
     */
    private static BeanDefinition createUserHolderBeanDefinition(){
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(UserHolder.class);
        builder.addConstructorArgReference("superUser");
        return builder.getBeanDefinition();
    }

/*    @Bean
    public UserHolder userHolder(User user) {
        return new UserHolder(user);
    }*/



}
